package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by MtRider on 2021/10/16 20:11
 */
public class SearchInsert {
    public static void main(String[] args) {
        //nums = [1,3,5,6], target = 5
        int [] nums={1,3,5,6};
        System.out.println(searchInsert(nums, 2));
    }
    public static int searchInsert(int[] nums, int target) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            list.add(nums[i]);
            //如果等于目标函数返回下标
            if(target == nums[i]){
                return i;
            }
            //否走插入数值Collection排序 在查找下标
            else if(i==nums.length-1){
                list.add(target);
                Collections.sort(list);
                int index = list.indexOf(target);
                return index;
            }
        }
        return -1;
    }
}
